package com.wangyadong.hobby.concurrent;

import com.wangyadong.hobby.thread.ReentrantLock;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

import static com.wangyadong.hobby.concurrent.CountDownLatchDemo.PRINT_CONSUMER;

public class PhaserDemo1 {
    private static final int num = 10;
//    private static final Phaser phaser = new Phaser(num + 1);

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock=new ReentrantLock();
        

        Phaser phaser = new Phaser();
        for (int i = 0; i < num; i++) {
            Thread thread = new Thread(() -> {
                try {
                    phaser.register();
                    PRINT_CONSUMER.accept("--------------子任务开始执行----------------");
                    TimeUnit.SECONDS.sleep(2);
                    PRINT_CONSUMER.accept("--------------子任务开始执行完毕-------------");
                    phaser.arrive();
                } catch (InterruptedException e) {
                    PRINT_CONSUMER.accept("--------------发生异常----------------");
                }
            });
            thread.start();
        }
        phaser.register();
        TimeUnit.SECONDS.sleep(0x4);
        phaser.arriveAndAwaitAdvance();
        PRINT_CONSUMER.accept(" 结束");
    }
}
